package com.whut.controller;

import com.whut.model.Manageinfo;
import com.whut.model.Orderinfo;
import com.whut.model.Userinfo;
import com.whut.service.UserService;
import com.whut.util.MD5Encoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ResponseBody
    @PostMapping("/insertUser")
    public String insertUser(Userinfo userinfo) {
        return userService.insertOne(userinfo);
    }

    @ResponseBody
    @PostMapping("/getUserById")
    public Userinfo getUserById(int mKey) {
        return userService.getUserById(mKey);
    }

    /***wxd***/
    @ResponseBody
    @PostMapping("/leave")
    public String leave(Manageinfo manageinfo) {
        return userService.leave(manageinfo);
    }

    @ResponseBody
    @PostMapping("/sign")
    public String sign(Manageinfo manageinfo) {
        return userService.sign(manageinfo);
    }

    @ResponseBody
    @PostMapping("/manage")
    public List<Object> manage(Integer id) {
        return userService.manage(id);
    }

    @ResponseBody
    @PostMapping("/getJobById")
    public  String getJobById(Integer id){
        return userService.getJobById(id);
    }

    @ResponseBody
    @PostMapping("/room")
    public List<Object> room() {
        return userService.room();
    }
    /***wxd end***/

    /**
     * @param
     * @return java.lang.Object
     * @auther wenyihan
     * 获取所有的用户信息
     * @date 21:22 2019/10/29
     */
    @ResponseBody
    @PostMapping("/getAllUserInfo")
    public Object getAllUserInfo() {
        return userService.getAllUserInfo();
    }



    /**
     * @auther wenyihan
     * 通过id查找userInfo和manageInfo表中的职员对其任职及任职时间进行修改
     * @date 10:10 2019/10/30
     * @param （id, serve, enterDate）
     * @return java.lang.Object
    */
    @ResponseBody
    @PostMapping("/editServeInfoById")
    public Object editServeInfoById(int id, String serve, String job){return userService.editServeInfoById(id, serve, job);}

    /**
     * @auther wenyihan
     * 获取所有的管理员姓名，显示在下拉框中
     * @date 11:05 2019/11/4
     * @return java.lang.Object
     */
    @ResponseBody
    @PostMapping("/getAllAdmin")
    public Object getAllAdmin(){return userService.getAllAdmin();}

    @ResponseBody
    @PostMapping("/Register")
    public Object Register(Userinfo user){
        Userinfo u = userService.getStaffInfoBytel(user.getTel());
        if (u!=null) {
            return "duplicate";
        }
        System.out.println(user.getPassword());
        user.setPassword(MD5Encoder.getMD5(user.getPassword()));//初始密码
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd ");
        String date = dateFormat.format(calendar.getTime());
        System.out.println(date);
        user.setEnterDate(date);
        return  userService.addUser(user);
    }

    @ResponseBody
    @PostMapping("/checkLogin")
    public Object checkLogin(String tel, String password){
        System.out.println(tel+password);
        return userService.checkPassword(tel,password);
    }

    /**
     * @return
     * @auther lyl  这不是我的方法啊啊 啊啊
     * 更改用户信息界面的用户信息
     * @date 11:10 2019/11/5
     */
    @ResponseBody
    @PostMapping("/getStaffInfo")
    public Userinfo getStaffInfo(String tel){
        return userService.getStaffInfoBytel(tel);
    }

    @ResponseBody
    @PostMapping("/getOrderInfo")
    public List<Object> getOrderInfo(){return userService.getOrderInfo();}

    /**
     * @return
     * @auther lyl
     * 更改用户信息界面的用户信息
     * @date 11:10 2019/11/5
     */
    @ResponseBody
    @PostMapping("/addUserInfo")
    public Object addUserInfo(Integer id, String nickName, String sex) {
        System.out.println(id + nickName + sex);
        return userService.addUserInfo(id, nickName, sex);
    }

    @ResponseBody
    @PostMapping("/getUserInfo")
    public Object addUserInfo(Integer id) {
        return userService.getUserInfo(id);
    }
}
